package com.example.newproject_overseas.ui.theme

import android.app.Activity
import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.Stable
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
import androidx.core.view.WindowCompat

private val DarkColorScheme = darkColorScheme(
    primary = Purple80,
    secondary = PurpleGrey80,
    tertiary = Pink80
)

private val LightColorScheme = lightColorScheme(
    primary = Purple40,
    secondary = PurpleGrey40,
    tertiary = Pink40

    /* Other default colors to override
    background = Color(0xFFFFFBFE),
    surface = Color(0xFFFFFBFE),
    onPrimary = Color.White,
    onSecondary = Color.White,
    onTertiary = Color.White,
    onBackground = Color(0xFF1C1B1F),
    onSurface = Color(0xFF1C1B1F),
    */
)

@Composable
fun NewProject_overseasTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    // Dynamic color is available on Android 12+
    dynamicColor: Boolean = true,
    content: @Composable () -> Unit
) {
    val colorScheme = when {
        dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
            val context = LocalContext.current
            if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
        }

        darkTheme -> DarkColorScheme
        else -> LightColorScheme
    }
    val view = LocalView.current
    if (!view.isInEditMode) {
        SideEffect {
            val window = (view.context as Activity).window
            window.statusBarColor = colorScheme.primary.toArgb()
            WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = darkTheme
        }
    }

    MaterialTheme(
        colorScheme = colorScheme,
        typography = Typography,
        content = content
    )
}

object BaseElementComposeTheme {
    val colors: BaseElementComposeColors
        @Composable
        get() = LocalWeComposeColors.current
    enum class Theme {
        Light, Dark, NewYear,Green
    }
}
private val LocalWeComposeColors = compositionLocalOf {
    LightColorPalette
}
private val LightColorPalette = BaseElementComposeColors(
    bottomBar = white1,//底部导航栏背景颜色
    background = white2,//主题背景颜色
    listItem = white,//lazyColumn列表项背景颜色
    divider = white3,//分界线颜色
    chatPage = white1,//聊天界面背景颜色
    textPrimary = black3,//文字：网名背景颜色，消息列表的名称（聊天界面：其他人发送的消息文字颜色）
    textPrimaryMe = black3,//聊天界面：我发送的消息文字颜色
    textSecondary = grey1,//文字；记录背景颜色，消息列表的消息
    onBackground = grey3,//qr_code背景颜色、联系人人界面分栏文字背景颜色
    icon = black,//底部我导航栏未被选择Icon颜色
    iconCurrent = green3,//底部我导航栏被选择Icon颜色
    badge = red1,//角标背景颜色：红色未读消息小圆圈
    onBadge = white,//角标内文字颜色
    bubbleMe = green1,//聊天界面，我发送的气泡消息背景颜色
    bubbleOthers = white,//聊天界面，其他人(非我)发送的气泡消息背景颜色
    textFieldBackground = white,//聊天界面：底部输入框背景颜色
    more = grey4,//右箭头背景颜色
    chatPageBgAlpha = 0f,//聊天界面透明度，如果切换红色主题，则为1，相应显示其图片，否则为0，图片无法显示
    searchIconColor = grey3,//搜索框搜索图标颜色
    searchTextColor = grey3,//搜索框搜索文字颜色
)
@Stable
class BaseElementComposeColors(
    bottomBar: Color,
    background: Color,
    listItem: Color,
    divider: Color,
    chatPage: Color,
    textPrimary: Color,
    textPrimaryMe: Color,
    textSecondary: Color,
    onBackground: Color,
    icon: Color,
    iconCurrent: Color,
    badge: Color,
    onBadge: Color,
    bubbleMe: Color,
    bubbleOthers: Color,
    textFieldBackground: Color,
    more: Color,
    chatPageBgAlpha: Float,
    searchIconColor: Color,
    searchTextColor: Color
) {

    var bottomBar: Color by mutableStateOf(bottomBar)
        private set
    var background: Color by mutableStateOf(background)
        private set
    var listItem: Color by mutableStateOf(listItem)
        private set
    var chatListDivider: Color by mutableStateOf(divider)
        private set
    var chatPage: Color by mutableStateOf(chatPage)
        private set
    var textPrimary: Color by mutableStateOf(textPrimary)
        private set
    var textPrimaryMe: Color by mutableStateOf(textPrimaryMe)
        private set
    var textSecondary: Color by mutableStateOf(textSecondary)
        private set
    var onBackground: Color by mutableStateOf(onBackground)
        private set
    var icon: Color by mutableStateOf(icon)
        private set
    var iconCurrent: Color by mutableStateOf(iconCurrent)
        private set
    var badge: Color by mutableStateOf(badge)
        private set
    var onBadge: Color by mutableStateOf(onBadge)
        private set
    var bubbleMe: Color by mutableStateOf(bubbleMe)
        private set
    var bubbleOthers: Color by mutableStateOf(bubbleOthers)
        private set
    var textFieldBackground: Color by mutableStateOf(textFieldBackground)
        private set
    var more: Color by mutableStateOf(more)
        private set
    var chatPageBgAlpha: Float by mutableStateOf(chatPageBgAlpha)
        private set
    var searchIconColor: Color by mutableStateOf(searchIconColor)
        private set
    var searchTextColor: Color by mutableStateOf(searchTextColor)
        private set
}